const Net = require('../../common.js')
Page({
  data: {
    hidden: true,
    id: '',
    price: '',
    goodsPrice: 0,
    goodsInfo: null,
    onSwitchStatus: true,
    onPriceStatus: false,
    priceStatus: false,
    anchorPrice: 0,
    anchorPriceStatus: false,
    anchorPrice: '',
  },
  onLoad: function (options) {
    Net.data({
      id: options.id,
      goodsPrice: options.price
    })
  },
  onChoiceGoods() {
    Net.PageRoter('_ChoiceGoods', '', 'nav')
  },
  onCreateActivity() {
    if (!Net.data('goodsInfo')) {
      Net.toast('请选择活动商品', 'none')
      return false
    }
    Net.link('v1/cases', {
      model_case_id: Net.data('id'),
      mall_goods_id: Net.data('goodsInfo').tao_id,
      reward_money: Net.data('onSwitchStatus') ? Net.data('price') ? Net.data('price') : 0 : 0,
      anchor_reward: Net.data('onPriceStatus') ? Net.data('anchorPrice') ? Net.data('anchorPrice') : 0 : 0
    }, function (res) {
      Net.PageRoter("_CreateActivitySuccess", "", "red")
    })
  },
  onPrice(e) {
    if (!Net.data('goodsInfo')) {
      Net.toast('请选择活动商品', 'none')
      Net.data({
        price: '',
        priceStatus: false
      })
    } else {
      if (e.detail.value > Number(Net.data('goodsInfo').size)) {
        Net.toast('佣金须小于商品价格', 'none')
        Net.data({
          price: Number(Net.data('goodsInfo').size)
        })
      } else {
        console.log(e.detail.value, Number(Net.data('goodsInfo').size))
        Net.data({
          price: Number(e.detail.value)
        })
      }
    }
  },
  onAnchorPrice(e) {
    if (!Net.data('goodsInfo')) {
      Net.toast('请选择活动商品', 'none')
      Net.data({
        anchorPrice: '',
        anchorPriceStatus: false
      })
    } else {
      if (e.detail.value > Number(Net.data('goodsInfo').size)) {
        Net.toast('佣金须小于商品价格', 'none')
        Net.data({
          anchorPrice: Number(Net.data('goodsInfo').size)
        })
      } else {
        Net.data({
          anchorPrice: Number(e.detail.value)
        })
      }

    }
  },
  onAnchorPriceFocus(e) {
    if (e.type == "focus") {
      Net.data({
        anchorPriceStatus: true
      })
    }
    if (e.type == "blur") {
      if (!e.detail.value) {
        Net.data({
          anchorPriceStatus: false
        })
      }
    }
  },
  onInputFocus(e) {
    if (e.type == "focus") {
      Net.data({
        priceStatus: true
      })
    }
    if (e.type == "blur") {
      if (!e.detail.value) {
        Net.data({
          priceStatus: false
        })
      }
    }
  },
  onSwitch(e) {
    if (e.currentTarget.id == 1) {
      Net.data({
        onSwitchStatus: !Net.data('onSwitchStatus')
      })
    } else {
      Net.data({
        onPriceStatus: !Net.data('onPriceStatus')
      })
    }

  },
  onReady: function () {},
  onShow: function () {},
  onHide: function () {},
  onUnload: function () {},
  onPullDownRefresh: function () {},
  onReachBottom: function () {},
  onShareAppMessage: function () {}
})